package com.yfbao.sharding.mybatisp.algorithm;

import lombok.extern.log4j.Log4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * 自定义分片算法
 * 
 * @author yinjihuan
 *
 */

public class MyPreciseShardingAlgorithm implements PreciseShardingAlgorithm<Integer> {


	@Override
	public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<Integer> shardingValue) {
		System.out.println("asdadasdadadasdasdasdas");
		Integer value = shardingValue.getValue();
		String logicTableName = shardingValue.getLogicTableName();
		System.out.println(logicTableName);
        String finalTableName = logicTableName+"_"+value%3;
		System.out.println(finalTableName);
		for (String tableName : availableTargetNames) {
			if(tableName.equalsIgnoreCase(finalTableName)){
				return tableName;
			}
//			if (tableName.endsWith(value % 4 + "")) {
//				return tableName;
//			}
		}
		throw new IllegalArgumentException();
	}

}
